package com.dsteiner.xelavos.client.controller;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import com.dsteiner.widgets.client.IJsUtil;
import com.dsteiner.widgets.client.JsUtil;
import com.dsteiner.widgets.client.ObjectUtil;
import com.dsteiner.widgets.client.RoundBorder.RoundBorderModel;
import com.dsteiner.widgets.client.Widgets.IHTML;
import com.dsteiner.widgets.client.Widgets.IImage;
import com.dsteiner.widgets.client.Widgets.ILazyWidget;
import com.dsteiner.widgets.client.Widgets.IWidget;
import com.dsteiner.widgets.client.Widgets.WidgetListenerAdapter;
import com.dsteiner.widgets.client.event.EventManagerImpl;
import com.dsteiner.widgets.client.resources.RoundBorderResources;
import com.dsteiner.xelavos.client.constants.Constants;
import com.dsteiner.xelavos.client.event.SearchEvent;
import com.dsteiner.xelavos.client.event.TabSelectionEvent;
import com.dsteiner.xelavos.client.model.DataModel;
import com.dsteiner.xelavos.client.model.MainTabModel;
import com.dsteiner.xelavos.client.model.SearchModel;
import com.dsteiner.xelavos.client.model.TabModel;
import com.dsteiner.xelavos.client.resources.XelavosResources;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.AbstractImagePrototype;

public class TabController {

	private TabModel tabModel;
	private MainTabModel mainTabModel;
	private IHTML link;
	private ILazyWidget tabWidget;
	private IImage tabImage;
	private IImage tabSelectedImage;
	private RoundBorderModel roundBorderModel;
	private IImage image;
	private SearchModel searchModel;
	private IJsUtil jsUtil;

	public void registerImage(IImage image) {
		this.image = image;
		image.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				EventManagerImpl.getInstance().fireEvent(new TabSelectionEvent(tabModel.getTab()));
				jsUtil.trackEvent(Constants.CAT_TAB, Constants.ACT_CLICKED_ICON, tabModel.getTab().getId()+"");
			}
		});
	}

	public void registerMainTabModel(MainTabModel mainTabModel) {
		this.mainTabModel = mainTabModel;
		mainTabModel.addPropertyChangeListener("selectedTab", new PropertyChangeListener(){
			public void propertyChange(PropertyChangeEvent evt) {
				updateText();
				updateImageVisibility();
				updateStylename();
			}
		});
	}

	public void registerRoundBorderModel(RoundBorderModel roundBorderModel) {
		this.roundBorderModel = roundBorderModel;
	}

	public void registerSearchModel(SearchModel searchModel) {
		this.searchModel = searchModel;
	}

	public void registerTab(ILazyWidget tabWidget) {
		this.tabWidget = tabWidget;
		tabWidget.addWidgetListener(new WidgetListenerAdapter(){
			@Override
			public void onAttach(IWidget widget) {
				updateText();
				updateImageVisibility();
				updateStylename();
				updateImage();
			}
		});
	}

	public void registerTabImage(IImage tabImage) {
		this.tabImage = tabImage;
	}

	public void registerTabLink(IHTML link) {
		this.link = link;
		link.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				EventManagerImpl.getInstance().fireEvent(new TabSelectionEvent(tabModel.getTab()));
				jsUtil.trackEvent(Constants.CAT_TAB, Constants.ACT_CLICKED_LINK, tabModel.getTab().getId()+"");
//				DataModel.SEARCH_REQUEST_DEFAULT.setClassName(mainTabModel.getSelectedTab().getClassName());
//				EventManagerImpl.getInstance().fireEvent(new SearchEvent(DataModel.SEARCH_REQUEST_DEFAULT));
			}
		});
	}

	public void registerTabModel(TabModel tabModel) {
		this.tabModel = tabModel;
		tabModel.addPropertyChangeListener("image", new PropertyChangeListener(){
			public void propertyChange(PropertyChangeEvent evt) {
				updateImage();
			}
		});
	}

	public void registerTabSelectedImage(IImage tabSelectedImage) {
		this.tabSelectedImage = tabSelectedImage;
	}

	private boolean isTabSelected() {
		return ObjectUtil.equals(mainTabModel.getSelectedTab(), tabModel.getTab());
	}

	private void updateImage() {
		AbstractImagePrototype abstractImagePrototype = tabModel.getImage();
		if(abstractImagePrototype != null)
			abstractImagePrototype.applyTo(image.getImage());
	}

	private void updateImageVisibility() {
		tabImage.setVisible(!isTabSelected());
		tabSelectedImage.setVisible(isTabSelected());
	}

	private void updateStylename() {
		tabWidget.setStyleName(XelavosResources.INSTANCE.css().tabWidget());
		if(isTabSelected())
			tabWidget.addStyleDependentName("selected");
		roundBorderModel.setCssResource(RoundBorderResources.Util.INSTANCE.css());
		if(isTabSelected())
			roundBorderModel.setCssResource(RoundBorderResources.Util.INSTANCE.cssRed());
	}

	private void updateText() {
		String text = tabModel.getText();
		if(isTabSelected())
			link.setHTML(text);
		else
			link.setHTML("<a href=\"javascript:void(0);\">"+text+"</a>");
	}

	public void registerJsUtil(IJsUtil jsUtil) {
		this.jsUtil = jsUtil;
	}

}
